package com.own.system.config.security;

import com.own.system.config.security.Service.CustomerUserDetailsService;
import com.own.system.config.security.filter.CheckTokenFilter;
import com.own.system.config.security.handler.AnonymousAuthenticationHandler;
import com.own.system.config.security.handler.CustomerAccessDeniedHandler;
import com.own.system.config.security.handler.LoginFailureHandler;
import com.own.system.config.security.handler.LoginSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@Configuration//标明是配置类
@EnableWebSecurity//启用Spring Security
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启权限注解，默认是关闭的
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    protected LoginSuccessHandler loginSuccessHandler;
    @Resource
    protected LoginFailureHandler loginFailureHandler;
    @Resource
    protected AnonymousAuthenticationHandler anonymousAuthenticationHandler;
    @Resource
    protected CustomerAccessDeniedHandler customerAccessDeniedHandler;
    @Resource
    private CustomerUserDetailsService customerUserDetailsService;
    @Resource
    private CheckTokenFilter check;

    /**
     * 注入密码加密方式
     *
     * @return
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    /**
     * 处理登录认证
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //登录前进行过滤
        http.addFilterBefore(check, UsernamePasswordAuthenticationFilter.class);
        //登录过程处理器
        http.formLogin()//表单登录
                .loginProcessingUrl("/api/user/login")//登录接口,不然慧一直404，并且登录接口不能为get请求，应该是post请求
                .successHandler(loginSuccessHandler)//登录成功处理器
                .failureHandler(loginFailureHandler)//登录失败处理器
                .usernameParameter("username")//登录参数名,如果表单内不是username,则需要修改
                .and()//禁用csrf
                .csrf()//禁用csrf
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)//禁用session
                .and()
                .authorizeHttpRequests()//设置需要拦截的请求
                .antMatchers("/api/user/login","/img/**").permitAll()//登录接口以及静态放行
                .anyRequest().authenticated()//其他请求都需要认证
                .and()
                .exceptionHandling()//异常处理
                .authenticationEntryPoint(anonymousAuthenticationHandler) //匿名无权访问
                .accessDeniedHandler(customerAccessDeniedHandler)//认证用户无权限访问
                .and()
                .cors();//允许跨域
    }

    /**
     * 配置认证管理器
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customerUserDetailsService).passwordEncoder(passwordEncoder());//配置用户认证服务,并且设置密码加密方式
    }
}
